#include "FlowController.h"
#include "common.h"

FlowController::FlowController(std::string devname, int interval, int threshold):
    Event(interval),
    m_devname(devname)
{
    m_threshold = threshold;
    m_send_passed = 0;
    m_recv_passed = 0;

    m_interval = interval / 1000;
}

FlowController::~FlowController()
{

}

bool FlowController::SRecordFlow(int datasize)
{
    if (0 == m_threshold.load())
    {
        m_send_passed += datasize;
        return true;
    }
    else if(m_threshold >= m_send_passed + datasize)
    {
        m_send_passed += datasize;
        return true;
    }
    m_drop += datasize;

    return false;
}

bool FlowController::RRecordFlow(int datasize)
{
    m_recv_passed += datasize;

    return true;
}

void FlowController::CycleTask()
{
    double send = m_send_passed.load();
    double recv = m_recv_passed.load();

    if (0.0 == send && 0.0 == recv) return;

    send = send / (1024 * m_interval);
    recv = recv / (1024 * m_interval);
    
    m_drop = 0;
    m_send_passed = 0;
    m_recv_passed = 0;

    LOGFMTI("flow monitoring record : %s, send rate : %.3f (KB/s), receive rate : %.3f (KB/s), drop data : %d",
        m_devname.c_str(), send, recv, m_drop.load()
    );
}